package com.zzmm.socket.demo;

import lombok.SneakyThrows;
import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class SocketTCPServerTest {

    String dir = "src/main/resources/";

    @SneakyThrows
    @Test
    public void testSocketServer01() {
        ServerSocket serverSocket = new ServerSocket(9999);

        // for (;;){
        Socket socket = serverSocket.accept();
        System.out.println("socket = " + socket);

        byte[] data = new byte[1024];
        int len;
        InputStream inputStream = socket.getInputStream();
        len = inputStream.read(data);
        System.out.println(new String(data, 0, len));

        // OutputStream outputStream = socket.getOutputStream();

        Thread.sleep(100000);
        System.out.println("睡醒了");

        // outputStream.write("Hello QQClient".getBytes(StandardCharsets.UTF_8));

        // socket.close();
        // }

    }

    @SneakyThrows
    @Test
    public void testSocketServer02() {
        ServerSocket serverSocket = new ServerSocket(9999);

        System.out.println("等待客户端连接...");
        Socket socket = serverSocket.accept();
        System.out.println("收到客户端连接请求，创建socket用于与客户端进行通信" + socket);

        // 接收客户端文件，并拷贝到resource
        System.out.println("服务端正在拷贝文件");
        byte[] data = new byte[1024];
        int len;
        BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(dir + "img_copy.png"));
        while ((len = bufferedInputStream.read(data)) != -1) {
            bufferedOutputStream.write(data, 0, len);
        }
        bufferedOutputStream.close();

        // 回复客户端已收到图片
        System.out.println("服务端正在回复客户端已收到图片");
        bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
        bufferedOutputStream.write("服务端收到客户端发来的图片".getBytes(StandardCharsets.UTF_8));
        bufferedOutputStream.flush();// 这里使用的是buffered所以需要手动刷新，否则数据不会通过底层流传输到网络
        socket.shutdownOutput();

        socket.close();
        serverSocket.close();
    }

    @SneakyThrows
    @Test
    public void testSocketServer03() {
        // 使用try-with-resources确保ServerSocket被正确关闭
        try (ServerSocket serverSocket = new ServerSocket(9999)) {
            System.out.println("服务器启动，等待客户端连接...");

            while (!serverSocket.isClosed()) {
                Socket socket = serverSocket.accept();
                System.out.println("收到客户端连接请求，创建socket用于与客户端进行通信" + socket);

                // 启动一个新线程来处理客户端请求
                new Thread(() -> {
                    try (OutputStream outputStream = socket.getOutputStream()) {
                        outputStream.write(("你好：" + socket).getBytes(StandardCharsets.UTF_8));
                        outputStream.flush();
                        socket.shutdownOutput(); // 通知客户端数据发送完毕
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }).start(); // 别忘了启动线程
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
